<?php
/**
 * @version		$Id: articles.php 17130 2010-05-17 05:52:36Z eddieajau $
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

/**
 * Methods supporting a list of article records.
 *
 * @package		Joomla.Administrator
 * @subpackage	com_content
 */
class AjaxWhoisModelExtensions extends JModelList
{
	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @since	1.6
	 */
	protected function populateState()
	{
		// Initialise variables.
		$app = JFactory::getApplication();

		// Adjust the context to support modal layouts.
		if ($layout = JRequest::getVar('layout', 'default')) {
			$this->context .= '.'.$layout;
		}

		$search = $app->getUserStateFromRequest($this->context.'.filter.title', 'filter_title');
		$this->setState('filter.title', $search);

		$group = $app->getUserStateFromRequest($this->context.'.filter.group', 'filter_group');
		$this->setState('filter.group', $group);

		$description = $app->getUserStateFromRequest($this->context.'.filter.description', 'filter_description');
		$this->setState('filter.description', $description);

		$category = $app->getUserStateFromRequest($this->context.'.filter.category', 'filter_category');
		$this->setState('filter.category', $category);

		$which = $app->getUserStateFromRequest($this->context.'.filter.which', 'filter_which');
		$this->setState('filter.which', $which);

		$continent = $app->getUserStateFromRequest($this->context.'.filter.continent', 'filter_continent');
		$this->setState('filter.continent', $continent);

		$country = $app->getUserStateFromRequest($this->context.'.filter.country', 'filter_country');
		$this->setState('filter.country', $country);

		// List state information.
		parent::populateState('a.orderingc', 'asc');
	}

	/**
	 * Returns a reference to the a Table object, always creating it.
	 *
	 * @param	type	The table type to instantiate
	 * @param	string	A prefix for the table class name. Optional.
	 * @param	array	Configuration array for model. Optional.
	 * @return	JTable	A database object
	 * @since	1.6
	 */
	public function getTable($type = 'Extensions', $prefix = 'AjaxWhoisTable', $config = array()) {
		return JTable::getInstance($type, $prefix, $config);
	}
	/**
	 * Method to get a store id based on model configuration state.
	 *
	 * This is necessary because the model is used by the component and
	 * different modules that might need different sets of data or different
	 * ordering requirements.
	 *
	 * @param	string		$id	A prefix for the store id.
	 *
	 * @return	string		A store id.
	 * @since	1.6
	 */
	protected function getStoreId($id = '')
	{
		// Compile the store id.
		$id	.= ':'.$this->getState('filter.title');
		$id .= ':'.$this->getState('filter.group');
		$id	.= ':'.$this->getState('filter.description');
		$id	.= ':'.$this->getState('filter.category');
		$id	.= ':'.$this->getState('filter.which');
		$id	.= ':'.$this->getState('filter.continent');
		$id	.= ':'.$this->getState('filter.country');

		return parent::getStoreId($id);
	}

	/**
	 * Build an SQL query to load the list data.
	 *
	 * @return	JDatabaseQuery
	 */
	protected function getListQuery()
	{
		// Create a new query object.
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		// Select the required fields from the table.
		$query->select(
			$this->getState(
				'list.select',
				'a.id, a.title, a.price, a.byePass, a.extGroup, a.extCat,' .
				'a.descr, a.continent, a.country, a.flag, ' .
				'a.incomponent, a.inmodule, a.orderingc, a.ordering, a.checkedc, a.checked, a.published')
		);
		$query->from('#__aw_ext AS a');

		// Filter by extension.
		if ($extension = $this->getState('filter.title')) {
			$extension = $db->Quote('%'.$db->getEscaped($extension, true).'%');
			$query->where('a.title LIKE ' . $extension);
		}

		// Filter by group.
		if ($group = $this->getState('filter.group')) {
			$group = $db->Quote('%'.$db->getEscaped($group, true).'%');
			$query->where('a.extGroup LIKE ' . $group);
		}

		// Filter by description.
		if ($description = $this->getState('filter.description')) {
			$description = $db->Quote('%'.$db->getEscaped($description, true).'%');
			$query->where('a.descr LIKE ' . $description);
		}

		// Filter by category.
		if ($category = $this->getState('filter.category')) {
			$category = $db->Quote('%'.$db->getEscaped($category, true).'%');
			$query->where('a.extCat LIKE ' . $category);
		}

		// Filter by which.
		$which = $this->getState('filter.which');
		switch ($which) {
			case "1":
				$query->where("(a.incomponent = '1' OR a.inmodule = '1')");
				break;
			case "2":
				$query->where("a.incomponent = '1'");
				break;
			case "3":
				$query->where("a.inmodule = '1'");
				break;
			case "0":
			default:
				break;
		}

		// Filter by continent.
		$continent = $this->getState('filter.continent');
		if ($continent=='none') {
			$query->where("TRIM(a.continent) = ''");
		} else if ($continent!='none' && $continent!='') {
			$query->where("TRIM(a.continent) = ".$continent);
		}
		
		// Filter by country.
		$country = $this->getState('filter.country');
		if ($country=='none') {
			$query->where("TRIM(a.country) = ''");
		} else if ($country!='none' && $country!='') {
			$query->where("TRIM(a.country) = ".$country);
		}
		
		// Add the list ordering clause.
		$orderCol	= $this->state->get('list.ordering');
		$orderDirn	= $this->state->get('list.direction');
		if ($orderCol == 'a.orderingc' || $orderCol == 'a.incomponent') {
			$orderCol = 'a.incomponent desc, a.orderingc';
		} else if ($orderCol == 'a.ordering' || $orderCol == 'a.inmodule') {
			$orderCol = 'a.inmodule desc, a.ordering';
		} else if ($orderCol == '') {
			$orderCol = 'a.incomponent desc, a.inmodule desc, a.orderingc, a.ordering';
			$orderDirn = '';
		}
		$query->order($db->getEscaped($orderCol.' '.$orderDirn));

		//echo nl2br(str_replace('#__','jos_',$query));
		return $query;
	}
	/**
	* Method to remove a item
	*
	* @access public
	* @return boolean True on success
	*/
	function delete($cid = array()) {
		$result = false;
		
		if (count( $cid )) {
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );
			$query = 'DELETE FROM #__aw_ext'
			. ' WHERE id IN ( '.$cids.' )';
			$this->_db->setQuery( $query );
			if(!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		return true;
	}

	/**
	* Method to (un)publish a item
	*
	* @access public
	* @return boolean True on success
	*/
	function publish($cid = array()) {
		$task = JRequest::getVar( 'task' );
		switch ($task) {
			case "publish":
				$field = "published";
				$publish = 1;
				break;
			case "unpublish":
				$field = "published";
				$publish = 0;
				break;
			case "incomponent":
				$field = "incomponent";
				$publish = 1;
				break;
			case "notincomponent":
				$field = "incomponent";
				$publish = 0;
				break;
			case "inmodule":
				$field = "inmodule";
				$publish = 1;
				break;
			case "notinmodule":
				$field = "inmodule";
				$publish = 0;
				break;
			case "checked":
				$field = "checked";
				$publish = 1;
				break;
			case "unchecked":
				$field = "checked";
				$publish = 0;
				break;
			case "checkedc":
				$field = "checkedc";
				$publish = 1;
				break;
			case "uncheckedc":
				$field = "checkedc";
				$publish = 0;
				break;
			case "byepass":
				$field = "byePass";
				$publish = 1;
				break;
			case "unbyepass":
				$field = "byePass";
				$publish = 0;
				break;
		}
				
		if (count( $cid )) {
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );
			
			$query = 'UPDATE #__aw_ext'
			. ' SET '.$field.' = '.(int) $publish
			. ' WHERE id IN ( '.$cids.' )'
			;
			$this->_db->setQuery( $query );
			if (!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}
		
		return true;
	}
	/**
	* Method to save ordering
	*
	* @access public
	* @return boolean True on success
	*/
	function saveorder($cid = array(), $order) {
		$task = JRequest::getVar( 'task' );
		if ($task == "saveorder") {
			$ordering = "ordering";
		} else {
			$ordering = "orderingc";
		}
		$row =& $this->getTable();
		
		// update ordering values
		for( $i=0; $i < count($cid); $i++ ) {
			$row->load( (int) $cid[$i] );
			if ($row->$ordering != $order[$i]) {
				$row->$ordering = $order[$i];
				if (!$row->store()) {
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}
	}
}